package com.xdcplus.web.service.alarm.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.dto.alarm.ToolAlarmGroupToolDTO;
import com.xdcplus.web.common.pojo.entity.alarm.ToolAlarmGroupTool;
import com.xdcplus.web.common.pojo.vo.alarm.FindToolAlarmGroupToolVO;
import com.xdcplus.web.common.pojo.vo.alarm.ToolAlarmGroupToolVO;
import com.xdcplus.web.common.pojo.vo.alarm.ToolSimpleVo;
import com.xdcplus.web.mapper.alarm.ToolAlarmGroupToolMapper;
import com.xdcplus.web.service.alarm.ToolAlarmGroupToolService;
import com.xdcplus.web.service.tool.ToolInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备告警组与设备关联表(ToolAlarmGroupTool)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-06-05 16:16:14
 */
@Slf4j
@Service("toolAlarmGroupToolService")
public class ToolAlarmGroupToolServiceImpl extends BaseServiceImpl<ToolAlarmGroupToolMapper, ToolAlarmGroupTool, ToolAlarmGroupTool, ToolAlarmGroupToolVO> implements ToolAlarmGroupToolService {

    @Autowired
    protected ToolAlarmGroupToolMapper toolAlarmGroupToolMapper;

    @Autowired
    private ToolInfoService toolInfoService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveToolAlarmGroupTool(ToolAlarmGroupToolDTO toolAlarmGroupToolDTO) {
        //        查询该分类的该区域下的设备ID
        List<Long> existsToolIdList= toolAlarmGroupToolMapper.queryToolIdByFactoryIdAndGroupIdFk(toolAlarmGroupToolDTO.getRegionId(),toolAlarmGroupToolDTO.getGroupIdFk());
        List<Long> needDeleteToolIdList = new ArrayList<>(existsToolIdList);
        needDeleteToolIdList.removeAll(toolAlarmGroupToolDTO.getToolIdFkList());
        if (CollUtil.isNotEmpty(needDeleteToolIdList)) {
            this.deleteByGroupIdFkAndToolIdFkList(toolAlarmGroupToolDTO.getGroupIdFk(), needDeleteToolIdList);
        }
        Timestamp nowTime=new Timestamp(DateUtil.current());
        List<Long> needAddToolIdList = new ArrayList<>(toolAlarmGroupToolDTO.getToolIdFkList());
        needAddToolIdList.removeAll(existsToolIdList);
        this.addToolAlarmGroupTool(toolAlarmGroupToolDTO.getGroupIdFk(),toolAlarmGroupToolDTO.getCreatedUser(), needAddToolIdList, nowTime);
    }

    private void deleteByGroupIdFkAndToolIdFkList(Long groupIdFk, List<Long> needDeleteToolIdList) {
        List<ToolAlarmGroupTool> toolAlarmGroupToolList=toolAlarmGroupToolMapper.queryByGroupIdFkAndToolIdFkList(groupIdFk, needDeleteToolIdList);
        if(CollUtil.isNotEmpty(toolAlarmGroupToolList)) {
            this.removeBatchByIds(toolAlarmGroupToolList);
        }
    }
    public void addToolAlarmGroupTool(Long groupIdFk, String createdUser,List<Long> needAddToolIdList, Timestamp nowTime) {
        List<ToolAlarmGroupTool> toolAlarmGroupToolList = new ArrayList<>();
        if (CollUtil.isNotEmpty(needAddToolIdList)) {
            for (Long toolId : needAddToolIdList) {
                ToolAlarmGroupTool toolAlarmGroupTool = new ToolAlarmGroupTool();
                toolAlarmGroupTool.setCreatedTime(nowTime);
                toolAlarmGroupTool.setCreatedUser(createdUser);
                toolAlarmGroupTool.setGroupIdFk(groupIdFk);
                toolAlarmGroupTool.setToolIdFk(toolId);
                toolAlarmGroupToolList.add(toolAlarmGroupTool);
            }
        }
        if(CollUtil.isNotEmpty(toolAlarmGroupToolList)){
            this.saveBatch(toolAlarmGroupToolList);
        }
    }
    @Override
    public void deleteByGroupIdFk(Long groupIdFk) {
        toolAlarmGroupToolMapper.deleteByGroupIdFk(groupIdFk);
    }

    @Override
    public FindToolAlarmGroupToolVO findToolAlarmGroupTool(Long groupIdFk, Long regionId) {
        FindToolAlarmGroupToolVO findToolAlarmGroupToolVO = new FindToolAlarmGroupToolVO();
        List<ToolSimpleVo> toolInfoList = toolInfoService.queryCurrentAndChildByFactoryId(regionId);
        List<Long> selectedToolInfoList = toolAlarmGroupToolMapper.queryToolIdByGroupIdFk(groupIdFk);
        if (toolInfoList == null) {
            toolInfoList = new ArrayList<>();
        }
        if (selectedToolInfoList != null) {
            List<ToolSimpleVo> unSelectedToolInfos = toolInfoList.stream()
                    .filter(toolInfo -> !selectedToolInfoList.contains(toolInfo.getId()))
                    .collect(Collectors.toList());
            List<ToolSimpleVo> selectedToolInfos = toolInfoList.stream()
                    .filter(toolInfo -> selectedToolInfoList.contains(toolInfo.getId()))
                    .collect(Collectors.toList());
            findToolAlarmGroupToolVO.setUnSelected(unSelectedToolInfos);
            findToolAlarmGroupToolVO.setSelectedTool(selectedToolInfos);
        } else {
            findToolAlarmGroupToolVO.setUnSelected(toolInfoList);
            findToolAlarmGroupToolVO.setSelectedTool(new ArrayList<>());
        }
        return findToolAlarmGroupToolVO;
    }

}
